// pages/interact/interact.js
const app = getApp() // getApp()获取App上的数据
const {jumpShow}=require('../../utils/util')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: "",
    title_disabled:true,//控制修改表头名字
    management_good:false,
    select_all:false,
    middlearr:[],
    items: []
  },
  // 改变类目的名字
  change_classname:function(){
     let that = this;
     that.setData({
       title_disabled: !that.data.title_disabled,
     });
    //  这里自动获取焦点
  },
  finish_classname: function () {
    let that = this;
    that.setData({
      title_disabled: !that.data.title_disabled,
    })
  },
  // 管理商品
  management:function(){
    let that = this;
    that.setData({
      management_good: true,
    })
  },
  finish_management:function(){
    let that = this;
    that.setData({
      management_good:false,
    })
  },
  itemId:[],
  // 选择
  select:function(e){
    var that = this;
    let arr2 = [];
    if (that.data.management_good == false){
       return;
    }else{
      var arr = that.data.items;
      var index = e.currentTarget.dataset.id; //数组的下标
      // console.log(index); 
      var videoId = arr[index].videoId; //选中视频的videoId
      // console.log(videoId);
      arr[index].checked = !arr[index].checked;
      // console.log(arr[index].checked)
      if(arr[index].checked == true){
        this.itemId.push(videoId);
      }
      else{
        this.itemId = this.itemId.filter((idx) => idx!=videoId )
      }
      // console.log(this.itemId)
      
      for(let i=0;i<arr.length;i++){
         if(arr[i].checked){
           arr2.push(arr[i])
         }
      };
      
      that.setData({
        items: arr,
        middlearr:arr2
      })
    }
  },

  //跳转播放视频
  jumpShow: function(e){
    jumpShow(e,'interact');
  },

  // 删除
  deleteitem:function(){
    var that = this;
    let arr = that.data.items;
    let arr2 = [];
    for(let i=0;i<arr.length;i++){
      if (arr[i].checked == false){
        arr2.push(arr[i]);
      }
    }
    console.log(arr2);
    that.setData({
      items:arr2,
      middlearr:[]
    })
    let userId = wx.getStorageSync('user').userId
    wx.request({
      url: `${app.globalData.url}/customer/batch/play_history?type=1&userId=${userId}`,
      method:'DELETE',
      header:{
        'content-type':'application/json'
      },
      data:{
        itemId:this.itemId
      },
      success: res=>{
        if(res.statusCode !== 200){
          console.log("删除失败")
        } else {
          // console.log(res.data)
          console.log(this.itemId+ " 删除成功")

        }
      },
    })

    
  },
  // 全选
  select_all:function(){
    let that = this;
    // console.log(that);
    console.log(that.items)
    for(let i=0;i<that.items.length;i++){
      this.itemId.push(that.items[i].videoId)
    }
    // console.log(this.itemId)

    that.setData({
      select_all: !that.data.select_all
    })
    if (that.data.select_all){
      let arr = that.data.items;
      let arr2 = [];
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].checked == true) {
          arr2.push(arr[i]);
        }else{
          arr[i].checked = true;
          arr2.push(arr[i]);
        }
      }
      that.setData({
        items: arr2,
        middlearr:arr2
      })
    }
  },
  // 取消全选
  select_none:function(){
    let that = this;
    that.setData({
      select_all: !that.data.select_all
    })
    this.itemId=[]
    console.log(this.itemId)
    let arr = that.data.items;
    let arr2 = [];
    for (let i = 0; i < arr.length; i++) {
        arr[i].checked = false;
        arr2.push(arr[i]);
    }
    that.setData({
      items: arr2,
      middlearr:[]
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList()
  },

  items:[],
  getList: function(){
    let userId = wx.getStorageSync('user').userId;

    wx.request({
      url: `${app.globalData.url}/customer/play_History?type=1&userId=${userId}`,
      method:'GET',
      success: res=>{
        for(let i=0; i<res.data.length; i++){
          var item={}
          item.videoId = res.data[i].videoId
          item.video_Title = res.data[i].video_Title
          item.video_Img = res.data[i].video_Img
          item.play_Time = res.data[i].play_Time
          item.checked = false
          this.items.push(item)
        }
        this.setData({
          items:this.items
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})